package blog

import (
	"fmt"
)

// 文章状态,自定义类型
type STATUS int

// Marshaler is the interface implemented by types that
// can marshal themselves into valid JSON.
//
//	type Marshaler interface {
//		MarshalJSON() ([]byte, error)
//	}
//
// 自定义当前类型的JSON输出
func (s STATUS) MarshalJSON() ([]byte, error) {
	switch s {
	case STATUS_DRAFT:
		return []byte(`"草稿"`), nil
	case STATUS_PUBLISH:
		return []byte(`"已发布"`), nil
	}
	return []byte(fmt.Sprintf("%d", s)), nil
}

// json 自定义反序列化
// 完成STATUS类型的自定义反序列化
func (s *STATUS) UnmarshalJSON(b []byte) error {
	switch string(b) {
	case `"草稿"`:
		*s = STATUS_DRAFT
		return nil
	case `"已发布"`:
		*s = STATUS_PUBLISH
		return nil
	}
	return nil
}

const (
	STATUS_DRAFT   STATUS = iota //默认状态0,即草稿状态
	STATUS_PUBLISH               //1发布
)
